import urllib.request
import urllib.error
import re
file = open("D:/maoyan.txt", "w")
for page in range(1,11):
    num = (page-1)*10
    url = "http://maoyan.com/board/4?offset="+str(num)
    try:
        content = urllib.request.urlopen(url).read().decode("utf-8","ignore")
        pat = '<i class="board-index.*?">(.*?)</i>.*?<a.*?>.*?<img data-src="(.*?)".*?/>.*?<p class="name"><a.*?>(.*?)</a>.*?<p class="star">.*?主演：(.*?)</p>.*?<p class="releasetime">上映时间：(.*?)</p>.*?<p class="score"><i class="integer">(.*?)</i><i class="fraction">(.*?)</i></p>'
        result = re.compile(pat,re.S).findall(content)
        for filmInfo in result:
            filmId = filmInfo[0]
            filmPic = filmInfo[1]
            filmName = filmInfo[2]
            filmStart = filmInfo[3].strip()
            filmTime = filmInfo[4]
            filmScore = filmInfo[5]+filmInfo[6]
            line = "序号："+filmId+" 电影名称："+filmName+" 主演："+filmStart+" 上演时间："+filmTime+" 评分："+filmScore+" 图片："+filmPic+'\n'
            file.write(line)

    except Exception as err:
        if hasattr(err, "code"):
            print("HTTPError")
            print(err.code + "-----" + err.reason)
        if hasattr(err, "reason"):
            print("URLError")
            print(err.reason)
file.close()
print("写入成功！！！")
